﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YJKStairs.CommonUnit;
using YJKStairs.DataInfos;

namespace YJKStairs.Manage
{
	public class CrossingStairsCreator : StairsCreator
	{
		public CrossingStairsCreator(ExternalCommandData commandData) : base(commandData)
		{
		}

		private void ChangeCrossingStairsBeamElevation(Autodesk.Revit.DB.Document doc, Stairs stairs, ref CrossingStairs param_stairs)
		{
			StairsRunManager stairsRunManager = new StairsRunManager(this.m_Document, stairs);
			double baseElevation = stairs.BaseElevation;
			double stairsTreadThickness = stairsRunManager.GetStairsTreadThickness();
			double stairsRunTopElevation = stairsRunManager.GetStairsRunTopElevation(0, baseElevation);
			base.SetBeamElevation(ref param_stairs.m_listBeamCurve, BeamType.BT_Mid, 0, stairsRunTopElevation - stairsTreadThickness);
		}

		private ElementId EditStairs(CrossingStairs param_stairs, ElementId bottomLevelId, ElementId topLevelId)
		{
			ElementId elementId = null;
			LineRunData leftUpRunData = param_stairs.m_LeftUpRunData;
			using (StairsEditScope stairsEditScope = new StairsEditScope(this.m_Document, "Stairs Automation"))
			{
				elementId = stairsEditScope.Start(bottomLevelId, topLevelId);
				Stairs stairs = this.m_Document.GetElement(elementId) as Stairs;
				using (Transaction transaction = new Transaction(this.m_Document, "Stairs Automation"))
				{
					transaction.Start();
					RunInfo runInfo = default(RunInfo);
					runInfo.RunWidth = param_stairs.m_dStairsWidth;
					runInfo.TreadDepth = leftUpRunData.TreadDepth;
					runInfo.TreadHeight = leftUpRunData.TreadHeight;
					runInfo.TreadThickness = leftUpRunData.TreadThickness;
					runInfo.RunStructalDepth = leftUpRunData.RunStructualDepth;
					runInfo.RiserThickness = leftUpRunData.RiserThickness;
					if (param_stairs.m_listSupport.Count > 0)
					{
						BaseSupportData baseSupportData = param_stairs.m_listSupport[0];
					}
					if (param_stairs.m_listRail.Count > 0)
					{
						BaseRaillingData baseRaillingData = param_stairs.m_listRail[0];
					}
					if (!base.ChangeStairsType(stairs, leftUpRunData.TheStairsType, runInfo, param_stairs.m_sName, param_stairs.m_LandData.LandingThickness))
					{
						transaction.RollBack();
						stairsEditScope.Cancel();
						return null;
					}
					try
					{
						double num = 0.0;
						double num2 = 0.0;
						int num3 = 0;
						foreach (SingleRunCurveInfo runConstructionInfo in param_stairs.m_listRunCurve)
						{
							if (num3 == 1 || num3 == 3)
							{
								num += num2;
							}
							else
							{
								num = 0.0;
							}
							StairsRun stairsRun = StairsRunCreator.CreateStairsRun(elementId, this.m_Document, num, runConstructionInfo);
							if (num3 == 0)
							{
								num2 = stairsRun.Height;
							}
							num3++;
						}
						foreach (LandingCurveInfo landingCurveInfo in param_stairs.m_listLandCurve)
						{
							double topElevation = num2;
							StairsLandingCreator.CreateLanding(stairs.Id, this.m_Document, topElevation, landingCurveInfo.m_arCurveInfo);
						}
						StairsUsingRangeXdata.WriteStairsUsingRange(stairs, param_stairs.m_sUsingRange);
					}
					catch (Exception)
					{
						transaction.RollBack();
						stairsEditScope.Cancel();
						return null;
					}
					XYZ vecTrans = new XYZ(param_stairs.m_TransVector.X, param_stairs.m_TransVector.Y, 0.0);
					base.TranslationStairs(stairs, vecTrans, param_stairs.m_ptRealInsert, param_stairs.m_dBottomOffset, param_stairs.m_dTopOffset);
					stairs.DesiredRisersNumber = stairs.ActualRisersNumber;
					transaction.Commit();
					double dSupportWidth = Common.MMToFeet(50.0);
					if (param_stairs.m_listSupport.Count > 0)
					{
						dSupportWidth = base.GetSupportTypeWidth(param_stairs.m_listSupport[0].SupportType);
					}
					base.SetDefaultSurpportType(stairs, param_stairs.m_listSupport);
					List<ElementId> stairsOutsupportIds = base.GetStairsOutsupportIds(stairs, param_stairs.m_listPointsSupport, dSupportWidth);
					base.ChangeSupportType(stairs, param_stairs.m_listSupport, stairsOutsupportIds);
				}
				stairsEditScope.Commit(new StairsEditScopeFailuresPreprocessor());
			}
			return elementId;
		}

		private double CalcStairsTopElevation(CrossingStairs clipperStairs)
		{
			return clipperStairs.m_dBottomOffset + clipperStairs.m_dLevelValue + clipperStairs.m_dBottomElevation;
		}

		public bool CreateCrossingStairs(CrossingStairs param_stairs)
		{
			bool result = true;
			ElementId elementId = null;
			ElementId elementId2 = null;
			double dBottomElevation = param_stairs.m_dBottomElevation;
			double topElevation = this.CalcStairsTopElevation(param_stairs);
			ElementId elementId3 = null;
			double num = 0.0;
			double num2 = 0.0;
			if (!base.GetLevel(dBottomElevation, topElevation, ref elementId, ref elementId2, ref num, ref num2, param_stairs.m_dLevelValue, ref elementId3) || !base.StairsLevelIsValid(elementId, elementId2))
			{
				return false;
			}
			num = param_stairs.m_dBottomOffset;
			num2 = param_stairs.m_dTopOffset;
			ElementId elementId4 = this.EditStairs(param_stairs, elementId, elementId2);
			if (!base.IsStairsValid(elementId4))
			{
				return false;
			}
			base.ShowStairsPath(elementId4, param_stairs.m_bNoteUpStairs);
			Stairs stairs = this.m_Document.GetElement(elementId4) as Stairs;
			if (!base.ChangeRailingType(stairs, param_stairs.m_listRail))
			{
				return false;
			}
			using (Transaction transaction = new Transaction(this.m_Document, "Stairs Beam"))
			{
				transaction.Start();
				List<string> arLandWidth = param_stairs.m_arLandWidth;
				string sStairsLand = ElementXDataManager.m_sStairsLand;
				if (arLandWidth != null)
				{
					base.WriteStairsLandWidthXData(stairs, "StairsXData", sStairsLand, arLandWidth);
				}
				this.ChangeCrossingStairsBeamElevation(this.m_Document, stairs, ref param_stairs);
				StairsBeamCreator.CreateStairsBeam(this.m_Document, this.m_RevitVersion, param_stairs.m_BeamData, param_stairs.m_listBeamCurve, param_stairs.m_TransVector, stairs, param_stairs.m_ptRealInsert);
				ElementXDataManager.CommitStairsXData(stairs);
				transaction.Commit();
			}
			base.RotateStairs(stairs, param_stairs.m_ptRealInsert);
			base.SetStairsBottomFloorVisibility(stairs, param_stairs.m_FloorType, this.m_Document.ActiveView, base.CreateStairBottomVisibleBuiltInCate());
			if (elementId3 != null)
			{
				base.DelLevel(elementId3);
			}
			return result;
		}
	}
}
